require("stacktrace")
local oldgetdebugstack = GLOBAL.getdebugstack --getdebugstack是获取调试堆栈的意思
local oldDoStackTrace = GLOBAL.DoStackTrace

--组包log
local function getenvinfo()
	local info = ""
	return info
end
--获取在线玩家信息
local function getplayerinfo()
	local info = ""
	for k, v in pairs(AllPlayers or {}) do --遍历所有玩家
		if k and v then
			info = info .. string.format("%s %s %s \n", v.name or "NoName", v.userid or "NoUserid", v.prefab or "UnKnow") --获取玩家名称和UID
		end
	end
	return info --return叫info的变量
end

local function getserverinfo()
	local TheWorld = GLOBAL.TheWorld
	local info = ""
	--平台和服务器信息
	info =
		info ..
		string.format(
			"PLATFORM : %s  IsServer: %s IsDedicated: %s World: %s  \n",
			PLATFORM or "UnKnow",
			tostring(TheNet:GetIsServer()),
			tostring(TheNet:IsDedicated()),
			TheWorld and TheWorld.prefab or "UnKnow"
		) --这里应该是判断服务器信息的，如果失败会赋值UnKnow
	--开启的mod
	if ModManager then
		for k, v in pairs(ModManager.mods) do --遍历MOD列表
			if k and v and v.modinfo then
				info =
					info ..
					string.format(
						"MOD : %s\t%s\t%s\t%s\n",
						v.modname or "UnKnow",
						v.modinfo.name or "UnKnowMod",
						v.modinfo.author or "UnKnowAuthor",
						v.modinfo.version or "UnKnow"
					) --检测MOD名称、作者以及版本
				if v.modname then
					local config, temp_options = KnownModIndex:GetModConfigurationOptions_Internal(v.modname, false)
					if config and type(config) == "table" then
						if type(temp_options) == "table" then
							for kc, kv in pairs(temp_options) do
								info = info .. string.format("\tTempOption  %s  : %s \n", kc, kv)
							end
						end
						for kc, kv in pairs(config) do
							info = info .. string.format("\tConfig  %s  : %s \n", tostring(kv.name), tostring(kv.saved or kv.default))
						end
					end
				end
			end
		end
	end
	--WorldState
	if TheWorld and TheWorld.state then
		info = info .. "WorldState：\n"
		for k, v in pairs(TheWorld.state) do
			info = info .. string.format("\t  %s  : %s  \n", tostring(k), tostring(v))
		end
	end
	return info
end

local function getserverlog()
	local info = "Can Read Server Log"
	local function onload(load_success, str)
		if load_success then
			info = str
		end
	end
	GLOBAL.TheSim:GetPersistentString("../server_log.txt", onload)
	return info
end
local function packlog(ret)
	local retlog = type(ret) == "table" and table.concat(ret, "\n") or ret
	local alllog =
		string.format(
		[[---------------------Envinfo---------------------------
%s
---------------------Playinfo---------------------------
%s
---------------------ServerInfo-------------------------
%s    
---------------------Traceback--------------------------
%s
---------------------Server_Log-------------------------
%s
]],
		getenvinfo(),
		getplayerinfo(),
		getserverinfo(),
		retlog,
		getserverlog()
	)

	return alllog
end
-- function GLOBAL.getdebugstack(res, start, top, bottom, ...)
-- 	start = (start or 1) + 2
-- 	local ret = oldgetdebugstack(res, start, top, bottom, ...)
-- 	local success, topost = pcall(packlog, ret)
-- 	--[[ if err then
-- 		topost = err .. "\n" .. table.concat(ret, "\n")
-- 	end ]]
-- 	logfile = io.open("../mods/error_report.log", "w")
-- 	logfile:write(packlog(ret))
-- 	logfile:close()

-- 	return ret
-- end

function GLOBAL.DoStackTrace(err)
	local retval = oldDoStackTrace(err)
	logfile = GLOBAL.io.open("../mods/musha_debug.log", "w")
	logfile:write(packlog(retval))
	logfile:close()
	return retval
end

--放 scripts/main 里
--modmain加 : modimport("scripts/scripts/main/logupload")
